package com.jamlu.framework.ui.status;

import com.blankj.utilcode.util.ActivityUtils;
import com.jamlu.framework.model.bean.BaseBean;
import com.jamlu.framework.model.bean.BaseListBean;
import com.jamlu.framework.network.NoNetworkException;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by DMing at 2018/2/28 0028
 */
public class RequestUtils {

    public static final int RESPOND_SUCCESS = 2000;//响应成功
    public static final int RESPOND_FAILURE = 2001;//响应失败
    public static final int TOKEN_EXPIRED = 2002;//token过期

    private Observable observable;
    private IStatus iStatus;
    

    private RequestUtils(Observable observable) {
        this.observable = observable;
    }

    public static RequestUtils request(@NonNull Observable observable){
        return new RequestUtils(observable);
    }

    public RequestUtils iStatus(@Nullable IStatus iStatus){
        this.iStatus = iStatus;
        return this;
    }

    public Disposable listener(){
        return new WrapRetrofit(iStatus).wrapNormalView(observable, null,null);
    }

    public Disposable listener(Consumer consumer){
        return new WrapRetrofit(iStatus).wrapNormalView(observable, (Consumer<BaseBean>) consumer,null);
    }

    public Disposable listener(Consumer consumer,Consumer<Integer> failureStatus){
        return new WrapRetrofit(iStatus).wrapNormalView(observable, (Consumer<BaseBean>) consumer,failureStatus);
    }

    public Disposable listenerList(){
        return new WrapRetrofit(iStatus).wrapRvView(observable,null,null);
    }

    public Disposable listenerList(Consumer consumer){
        return new WrapRetrofit(iStatus).wrapRvView(observable, (Consumer<BaseListBean>) consumer,null);
    }

    public Disposable listenerList(Consumer consumer,Consumer<Integer> failureStatus){
        return new WrapRetrofit(iStatus).wrapRvView(observable, (Consumer<BaseListBean>) consumer,failureStatus);
    }

    public static class WrapRetrofit {

        private IStatus iStatus;

        WrapRetrofit(IStatus iStatus) {
            this.iStatus = iStatus;
        }

        <T extends BaseBean> Disposable wrapNormalView(Observable<T> observable, final Consumer<T> consumer,final Consumer<Integer> failureStatus){
            return observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<T>() {
                        @Override
                        public void accept(T t) throws Exception {
                            if (t.getHttpCode() == RESPOND_SUCCESS){
                                if (iStatus != null)iStatus.switchStatus(IStatus.SUCCESS);
                                if (consumer != null)consumer.accept(t);
                            }else if (t.getHttpCode() == RESPOND_FAILURE){
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_RESPOND,t.getMsg());
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_RESPOND);
                            }else if (t.getHttpCode() == TOKEN_EXPIRED){
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_RESPOND,t.getMsg());
                                ActivityUtils.finishAllActivities(); //关闭所有页面
                            }else {
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_NETWORK);
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            if (throwable instanceof NoNetworkException){
                                if (iStatus != null)iStatus.switchStatus(IStatus.NO_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.NO_NETWORK);
                            }else {
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_NETWORK);
                            }
                        }
                    });
        }

        <T extends BaseListBean> Disposable wrapRvView(Observable<T> observable, final Consumer<T> consumer,final Consumer<Integer> failureStatus){
            return observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<T>() {
                        @Override
                        public void accept(T t) throws Exception {
                            if (t.getHttpCode() == RESPOND_SUCCESS){
                                if (iStatus != null)iStatus.switchStatus(IStatus.SUCCESS, t.getList());//List空数据与有数据,都进入
                                if (consumer != null)consumer.accept(t);
                            }else if (t.getHttpCode() == RESPOND_FAILURE){
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_RESPOND,t.getMsg());
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_RESPOND);
                            }else if (t.getHttpCode() == TOKEN_EXPIRED){
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_RESPOND,t.getMsg());
                                ActivityUtils.finishAllActivities(); //关闭所有页面
                            }else {
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_NETWORK);
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            if (throwable instanceof NoNetworkException){
                                if (iStatus != null)iStatus.switchStatus(IStatus.NO_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_NETWORK);
                            }else {
                                if (iStatus != null)iStatus.switchStatus(IStatus.ERROR_NETWORK);
                                if (failureStatus != null)failureStatus.accept(IStatus.ERROR_NETWORK);
                            }
                        }
                    });
        }
    }



}
